package x.zero;


import org.beetl.core.ReThrowConsoleErrorHandler;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.H2Style;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.loader.SQLLoader;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.gen.BaseProject;
import org.beetl.sql.gen.SourceBuilder;
import org.beetl.sql.gen.SourceConfig;
import org.beetl.sql.gen.simple.*;
import x.zero.gen.ZeroEntitySourceBuilder;
import x.zero.gen.ZeroMDSourceBuilder;
import x.zero.gen.ZeroMapperSourceBuilder;
import x.zero.gen.ZeroMavenProject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuhao
 */
public class BeetsqlGenerator {


    public static SQLManager getSQLManager() {
        String url = "jdbc:mysql://localhost:3306/zero?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false";
        ConnectionSource source = ConnectionSourceHelper.getSimple("com.mysql.cj.jdbc.Driver", url, "root", "1q2w3e4r");
        SQLManagerBuilder builder = new SQLManagerBuilder(source);
//设置NameConversion，这里数据库命名采用下划线风格，使用UnderlinedNameConversion
        builder.setNc(new UnderlinedNameConversion());
//设置一个拦截器，输出debug日志，包含了sql语句和执行参数，执行时间
        builder.setInters(new Interceptor[]{new DebugInterceptor()});
//设置数据库分隔，必须跟数据库一样
        builder.setDbStyle(new MySqlStyle());
        SQLManager sqlManager = builder.build();
        return sqlManager;
    }
    public static void main(String[] args) throws Exception{
        List<SourceBuilder> sourceBuilder = new ArrayList<>();
        SourceBuilder entityBuilder = new ZeroEntitySourceBuilder("pojo.entity");
        SourceBuilder mapperBuilder = new ZeroMapperSourceBuilder("mapper");
//        SourceBuilder mdBuilder = new ZeroMDSourceBuilder();

        sourceBuilder.add(entityBuilder);
        sourceBuilder.add(mapperBuilder);
//        sourceBuilder.add(mdBuilder);

        SourceConfig config = new SourceConfig(getSQLManager(),sourceBuilder);
//如果有错误，抛出异常而不是继续运行1
        EntitySourceBuilder.getGroupTemplate().setErrorHandler(new ReThrowConsoleErrorHandler() );

        //控制台
        ConsoleOnlyProject project = new ConsoleOnlyProject();
        String[] tableName = {
                "member_footprint"
        };

        //maven生成
        BaseProject zeroMavenProject = new ZeroMavenProject();

        for (String s : tableName) {
            config.gen(s,zeroMavenProject);
        }


    }


}
